<?php

/**
 * ListController.class.php 博客文章管理
 * Created Time 2016年3月11日 下午5:37:23
 *
 * @author     codingyuan@163.com
 */
namespace Home\Controller\Admin;

use Home\Common\AdminController;

class IndexController extends AdminController {

    protected $objServ;
    protected $objListServ;
    protected $limit = 20;
    // 初始化控制器，在具体应用控制器中尽量使用thinkphp提供的初始化工具
    public function _initialize()
    {
        $this->objServ = D ('Home/Article', 'Service');
        $this->objListServ = D('Home/List', 'Service');
    }
    
    public function index()
    {
        $page = rintval(I('get.p', 1));
        $start = null;
        $limit = $this->limit;
        list($start, $limit, $page) = page_limit($page, $limit);
        
        $count = null;
        $list = array();
        $order = 'aid desc';
        $search = I('get.');
        $this->__proFilter($search);
        $data = $this->objListServ->getAdminArticleList($search, $start, $limit, $order);
        list($count, $list) = $data;
        
        $this->_assignPage($count, $limit, $search);
        $this->__assignCate();
        $this->assign('count', $count);
        $this->assign('list', $list);
        $search['act_url'] = U ('Home/Admin/Index/index');
        $this->assign('search', $search);
        $this->loadSystemView ();
    }

    public function plus()
    {
        $this->contentTitle = '添加文章';
        $this->_assignUeditor ();
        $this->__assignCate();
        $actUrl = U ('Home/Admin/Index/dosubmit');
        $this->assign ('act_url', $actUrl);
        $this->loadSystemView ('edit');
    }

    public function edit()
    {
        $id = I('get.id');
        $this->contentTitle = '编辑文章';
        try
        {
            // 获取要编辑的数据
            $data = $this->objServ->getEditRow($id);
            $data['id'] = $id;
            $this->assign('data',$data);
            // 传递附件id
            if($data['face_atid'])
            {
                $this->assign('attids', $data['face_atid']);
            }
            
            $actUrl = U ('Home/Admin/Index/dosubmit');
            $this->assign ('act_url', $actUrl);
            $this->_assignUeditor (htmlspecialchars_decode($data['content']));
            $this->__assignCate();
            
        } catch ( \Exception $e )
        {
            $this->error($e->getMessage ());
            return save_app_log ($e->getMessage (), $this->identifier);
        }
        
        $this->loadSystemView ();
    }

    public function del()
    {
        return $this->error('删除失败');
        
    }

    public function dosubmit()
    {
        try
        {
            if (! IS_POST)
            {throw new \Exception (L('_ERR_FORMART_METHOD_'), array('method'=>'post'));}
            
            $postData = I ('post.');
            $this->objServ->saveData ($postData, $id);
            $jumpUrl = U('Home/Admin/Index/index');
            return $this->success('成功保存', $jumpUrl);
        } catch ( \Exception $e )
        {
            return $this->error ($e->getMessage ());
        }
    }
    
    private function __assignCate()
    {
        $objServ = D('Home/Category', 'Service');
        $data = $objServ->getCatList('cname,cid,pid');
        if(!$data)
        {
            $this->error(L('_ERR_EMPTY_CAT_'), U('Home/Admin/Catgory/setcat'));
        }
        
        $data = $objServ->proCatList($data);
        $this->assign('catlist', $data);
        
        return true;
    }

    private function __proFilter(&$search)
    {
        if(!$search || !is_array($search))
        {
            return false;
        }
        
        foreach ($search as &$val)
        {
            $val = urldecode($val);
        }
        
        return true;
    }
}

